﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessHandler;
using EasyCode.Utility;

public partial class Page_ManageCategory : System.Web.UI.Page
{
    private CategoryController categoryController;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //this.LoadCategory();
            this.GetCategory();
        }
    }
    private void GetCategory()
    {
        categoryController=CategoryController.Instance;
        Dictionary<int, string> myDic=new Dictionary<int, string>();
        int iCount=0;
        Dictionary<int, string> objListCat=BuilTreeList(ref myDic, ref iCount, categoryController.GetCategoryforInsertNews());
        this.Repeater_Content.DataSource=objListCat;
        this.Repeater_Content.DataBind();
    }
    protected Dictionary<int, string> BuilTreeList(ref Dictionary<int, string> myDic, ref int iCount, List<Category> myList)
    {
        this.categoryController=CategoryController.Instance;
        List<Category> allChild=categoryController.GetAllChildCat();
        foreach (Category item in myList)
        {
            int addKey=Protector.Int(item.CategoryID);
            string addValue=GetStringName(0)+HttpUtility.HtmlDecode(item.CategoryName);
            myDic.Add(addKey, "<strong>"+addValue+"</strong>");
            List<Category> objListChild=(from o in allChild
                                         where o.CategoryParentID==addKey
                                         select o).ToList();
            if (objListChild.Count>0)
            {
                iCount++;
                BuilTreeListChild(allChild, addKey, ref myDic, ref iCount);
                iCount--;
            }
        }
        if (myDic!=null&&myList.Count>0)
        {
            return myDic;
        }
        else
        {
            return new Dictionary<int, string>();
        }
    }
    protected void BuilTreeListChild(List<Category> myListOriginal, int parentID, ref Dictionary<int, string> myDic, ref int iCount)
    {
        List<Category> myList=(from o in myListOriginal
                               where o.CategoryParentID==parentID
                               select o).ToList();
        if (myList!=null)
        {
            foreach (Category item in myList)
            {
                int addKey=Protector.Int(item.CategoryID);
                string addValue=GetStringName(iCount)+HttpUtility.HtmlDecode(item.CategoryName);
                myDic.Add(addKey, addValue);
                List<Category> objListChild=(from o in myListOriginal
                                             where o.CategoryParentID==addKey
                                             select o).ToList();
                if (objListChild.Count>0)
                {
                    iCount++;
                    BuilTreeListChild(myListOriginal, addKey, ref myDic, ref iCount);
                    iCount--;
                }
            }
        }
    }
    protected string GetStringName(int levelID)
    {
        string abc="";
        for (int o=0; o<levelID; o++)
        {
            abc+="|-- ";
        }
        return abc;
    }
    protected void Repeater_Content_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id=Protector.Int(e.CommandArgument, 0);
        categoryController=CategoryController.Instance;
        var c=(from item in categoryController.Categories where item.CategoryID==id select item).SingleOrDefault();
        if (c!=null)
        {
            if (e.CommandName=="DeleteCommand")
            {
                if (!CheckRoles.CanDelete())
                {
                    Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
                }
                try
                {
                    categoryController.Categories.DeleteOnSubmit(c);
                    categoryController.SubmitChanges();
                    Message_Alert.SetSuccess(Resources.Category.DeleteSuccess);
                    this.GetCategory();
                }
                catch (Exception ex)
                {
                    Message_Alert.SetError(Resources.Category.DeleteFail+" Error: "+ex.Message);
                }
            }
            else if (e.CommandName=="Update")
            {
                if (!CheckRoles.CanEdit())
                {
                    Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
                }
                TextBox TextBox_Name=e.Item.FindControl("TextBox_Name") as TextBox;
                c.CategoryName=TextBox_Name.Text;
                try
                {
                    categoryController.SubmitChanges();
                    Message_Alert.SetSuccess(Resources.Category.UpdateSuccess);
                    this.GetCategory();
                }
                catch (Exception ex)
                {
                    Message_Alert.SetError(Resources.Category.UpdateFail+" Error: "+ex.Message);
                }
            }
        }
        if (e.CommandName=="Edit")
        {
            this.HideAllTextBox();
            TextBox TextBox_Name=e.Item.FindControl("TextBox_Name") as TextBox;
            LinkButton LinkButton_Edit=e.Item.FindControl("LinkButton_Edit") as LinkButton;
            LinkButton LinkButton_Save=e.Item.FindControl("LinkButton_Save") as LinkButton;
            LinkButton LinkButton_Cancel=e.Item.FindControl("LinkButton_Cancel") as LinkButton;
            Literal Literal_CategoryName=e.Item.FindControl("Literal_CategoryName") as Literal;
            if (LinkButton_Cancel!=null)
            {
                LinkButton_Cancel.Visible=true;
            }
            if (LinkButton_Save!=null)
            {
                LinkButton_Save.Visible=true;
            }
            if (LinkButton_Edit!=null)
            {
                LinkButton_Edit.Visible=false;
            }
            if (TextBox_Name!=null)
            {
                TextBox_Name.Text=this.Name(Literal_CategoryName.Text);
                TextBox_Name.Visible=true;
            }
        }
        else if (e.CommandName=="Cancel")
        {
            this.HideAllTextBox();
        }
    }
    private void HideAllTextBox()
    {
        for (int i=0; i<this.Repeater_Content.Items.Count; i++)
        {
            TextBox TextBox_Name=Repeater_Content.Items[i].FindControl("TextBox_Name") as TextBox;
            LinkButton LinkButton_Save=Repeater_Content.Items[i].FindControl("LinkButton_Save") as LinkButton;
            LinkButton LinkButton_Cancel=Repeater_Content.Items[i].FindControl("LinkButton_Cancel") as LinkButton;
            LinkButton LinkButton_Edit=Repeater_Content.Items[i].FindControl("LinkButton_Edit") as LinkButton;
            if (LinkButton_Cancel!=null)
            {
                LinkButton_Cancel.Visible=false;
            }
            if (LinkButton_Edit!=null)
            {
                LinkButton_Edit.Visible=true;
            }
            if (LinkButton_Save!=null)
            {
                LinkButton_Save.Visible=false;
            }
            if (TextBox_Name!=null)
            {
                TextBox_Name.Visible=false;
            }
        }
    }
    protected void UpdatePanelContent_Load(object sender, EventArgs e)
    {
    }
    public string Name(object catName)
    {
        if (catName!=null)
        {
            string name=catName.ToString();
            int lastIndex=name.LastIndexOf("|--");
            if (lastIndex>=0)
            {
                if (lastIndex == 0)
                {
                    lastIndex = 3;
                }
                else
                {
                    lastIndex = lastIndex + 3;
                }
                 
                return name.Substring(lastIndex, name.Length-lastIndex).Trim();
            }
            return name.Replace("<strong>", string.Empty).Replace("</strong>", string.Empty);
        }
        return string.Empty;
    }
    //private void LoadCategory()
    //{
    //    categoryController = CategoryController.Instance;
    //    this.Repeater_Content.DataSource = categoryController.GetAllCat();
    //    this.Repeater_Content.DataBind();
    //    if (this.Repeater_Content.Items.Count < 1)
    //    {
    //        this.Repeater_Content.Visible = false;
    //        this.tr_not_found.Visible = true;
    //    }
    //}
    protected void Repeater_Content_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
        {
            //((RequiredFieldValidator)e.Item.FindControl("RequiredFieldValidator_Valid")).ValidationGroup=((TextBox)e.Item.FindControl("TextBox_Name")).UniqueID;
            //((LinkButton)e.Item.FindControl("LinkButton_Save")).ValidationGroup=((TextBox)e.Item.FindControl("TextBox_Name")).UniqueID;
        }
    }
}